package com.guigu.equipmentservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.R;
import com.guigu.equipmentservice.dto.EquiImageDto;
import com.guigu.equipmentservice.dto.EquilVideoDto1;
import com.guigu.equipmentservice.dto.EquilVideoDto2;
import com.guigu.equipmentservice.dto.EquipmentEditDtoPC;
import com.guigu.equipmentservice.entity.Equipment;
import com.guigu.equipmentservice.entity.EquipmentImage;
import com.guigu.equipmentservice.entity.EquipmentType;
import com.guigu.equipmentservice.entity.EquipmentVideo;
import com.guigu.equipmentservice.feign.MyUserFeign;
import com.guigu.equipmentservice.service.EquipmentImageService;
import com.guigu.equipmentservice.service.EquipmentService;
import com.guigu.equipmentservice.service.EquipmentTypeService;
import com.guigu.equipmentservice.vo.BusinessmanEquipmentVo;
import com.guigu.equipmentservice.vo.EquipmentVo;
import com.guigu.equipmentservice.service.EquipmentVideoService;
import com.guigu.equipmentservice.vo.UserCollectionEquipmentVoPC;
import com.guigu.userservice.entity.User;
import com.guigu.userservice.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
/**
 * <p>
 * 设备表（存储卖家发布的二手机械信息） 前端控制器
 * </p>
 *
 * @author zcy
 * @since 2025-09-28
 */
@RestController
@RequestMapping("/equipmentservice/equipment")
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private EquipmentTypeService equipmentTypeService;

    @Autowired
    private EquipmentImageService equipmentImageService;

    @Autowired
    private EquipmentVideoService equipmentVideoService;

    @Autowired
    private MyUserFeign myUserFeign;


    /**
     * 前台-查询前8个设备品牌
     * */
    @GetMapping("/selectfrontbaEquipmentBrand")
    public R selectfrontbaEquipmentBrand(){

        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("status",1);
        equipmentQueryWrapper.last("LIMIT 8");
        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        return R.ok().data("list",list);
    }


    /**
     * 前台-查询所有设备品牌名字
     * */
    @GetMapping("/selectallEquipmentBrand")
    public R selectallEquipmentBrand(){

        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("status",1);
        equipmentQueryWrapper.select("DISTINCT brand");
        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        return R.ok().data("list",list);
    }
    //前台-查询前8个一级分类-我要卖
    @GetMapping("/selectfrontshiEquipmentType")
    public R selectfrontshiEquipmentType(){
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("status",1);
        equipmentQueryWrapper.select("DISTINCT location");
        equipmentQueryWrapper.last("LIMIT 8");
        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);
        return R.ok().data("list",list);
    }

    //前台-查询所有设备-我要买
    @PostMapping("/queryEquipmentPage/{pageNo}/{pageSize}")
    public R queryEquipmentPage(@PathVariable Integer pageNo,
                                @PathVariable Integer pageSize,
                                @RequestBody EquipmentVo vo) {
        PageInfo<Equipment> list = equipmentService.queryEquipmentPage(pageNo, pageSize, vo);
        return R.ok().data("list",list);
    }

    //前台-根据品牌查询型号-我要卖
    @GetMapping("/queryModelsByBrand")
    public R queryModelsByBrand(@RequestParam("brand") String brand) {
        QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
        wrapper.eq("status",1);
        wrapper.select("DISTINCT model");
        wrapper.eq("brand",brand);
        List<Equipment> models = equipmentService.list(wrapper);
        return R.ok().data("models",models);
    }

    //前台-查询前8个品牌-我要买
    @GetMapping("/queryEquipmentBrands")
    public R queryEquipmentBrands(){
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("status",1);
        equipmentQueryWrapper.select("DISTINCT brand");
        equipmentQueryWrapper.last("LIMIT 8");
        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        return R.ok().data("list",list);
    }

    /**
     * 前台-查询前14个设备品牌
     * */
    @GetMapping("/selectfrontshisiEquipmentBrand")
    public R selectfrontshisiEquipmentBrand(){

        List<Equipment> equipmentbrand = equipmentService.selectfrontshisiEquipmentBrand();

        return R.ok().data("equipmentbrand",equipmentbrand);
    }



    /**
     * 前台-查询第一个品牌名称
     * */
    @GetMapping("/selectoneequipmentbrand")
    public R selectoneequipmentbrand() {

        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("status",1);
        equipmentQueryWrapper.last("LIMIT 1");

        Equipment equipment = equipmentService.getOne(equipmentQueryWrapper);

        return R.ok().data("equipment",equipment);
    }

    /**
     * 前台-查询对应品牌的设备信息
     * */
    @GetMapping("/selectEquipmentInfoByBrand/{brand}")
    public R selectEquipmentInfoByBrand(@PathVariable("brand") String brand){


        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
         equipmentQueryWrapper.eq("status",1);
         equipmentQueryWrapper.eq("brand",brand);
         equipmentQueryWrapper.last("LIMIT 8");

        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        for (int i=0;i<list.size();i++){
            Equipment equipment = list.get(i);

            //根据设备类型id去查询设备类型表
            EquipmentType equipmentType = equipmentTypeService.getById(equipment.getTypeId());
            equipment.setTypeName(equipmentType.getTypeName());

            //根据设备id查询设备图片
            QueryWrapper<EquipmentImage> equipmentImageQueryWrapper = new QueryWrapper<>();
            equipmentImageQueryWrapper.eq("equipment_id",equipment.getEquipmentId());
            List<EquipmentImage> list1 = equipmentImageService.list(equipmentImageQueryWrapper);
            if (!list1.isEmpty()) {
                // 获取第一个设备图片
                EquipmentImage firstImage = list1.get(0);
                equipment.setImageUrl(firstImage.getImageUrl());
            }

            //根据设备id查询设备视频
            QueryWrapper<EquipmentVideo> equipmentVideoQueryWrapper = new QueryWrapper<>();
            equipmentVideoQueryWrapper.eq("equipment_id",equipment.getEquipmentId());
            List<EquipmentVideo> equipmentVideoList = equipmentVideoService.list(equipmentVideoQueryWrapper);
            if (!equipmentVideoList.isEmpty()) {
                // 获取第一个设备视频
                EquipmentVideo equipmentVideo = equipmentVideoList.get(0);
                equipment.setVideoUrl(equipmentVideo.getVideoUrl());
            }
        }
        return R.ok().data("list",list);
    }


    /**
     * 前台-查询前8个最新上架的设备
     * */
    @GetMapping("/selectEquipmentBycreateTime")
    public R selectEquipmentBycreateTime() {

        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("status",1);
        equipmentQueryWrapper.orderByDesc("release_time");
        equipmentQueryWrapper.last("LIMIT 8");

        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        for (int i=0;i<list.size();i++){
            Equipment equipment = list.get(i);

            //根据设备类型id去查询设备类型表
            EquipmentType equipmentType = equipmentTypeService.getById(equipment.getTypeId());
            equipment.setTypeName(equipmentType.getTypeName());

            //根据设备id查询设备图片
            QueryWrapper<EquipmentImage> equipmentImageQueryWrapper = new QueryWrapper<>();
            equipmentImageQueryWrapper.eq("equipment_id",equipment.getEquipmentId());
            List<EquipmentImage> list1 = equipmentImageService.list(equipmentImageQueryWrapper);
            if (!list1.isEmpty()) {
                // 获取第一个设备图片
                EquipmentImage firstImage = list1.get(0);
                equipment.setImageUrl(firstImage.getImageUrl());
            }

            //根据设备id查询设备视频
            QueryWrapper<EquipmentVideo> equipmentVideoQueryWrapper = new QueryWrapper<>();
            equipmentVideoQueryWrapper.eq("equipment_id",equipment.getEquipmentId());
            List<EquipmentVideo> equipmentVideoList = equipmentVideoService.list(equipmentVideoQueryWrapper);
            if (!equipmentVideoList.isEmpty()) {
                // 获取第一个设备视频
                EquipmentVideo equipmentVideo = equipmentVideoList.get(0);
                equipment.setVideoUrl(equipmentVideo.getVideoUrl());
            }
        }
        return R.ok().data("list",list);
    }

    /**
     * 前台-查询前8个成交案例
     * */
    @GetMapping("/selectfrontbaEquipmentByChengJiao")
    public R selectfrontbaEquipmentByChengJiao() {

        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("status",3);
        equipmentQueryWrapper.orderByDesc("release_time");
        equipmentQueryWrapper.last("LIMIT 8");

        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        for (int i=0;i<list.size();i++){
            Equipment equipment = list.get(i);

            //根据设备类型id去查询设备类型表
            EquipmentType equipmentType = equipmentTypeService.getById(equipment.getTypeId());
            equipment.setTypeName(equipmentType.getTypeName());

            //根据设备id查询设备图片
            QueryWrapper<EquipmentImage> equipmentImageQueryWrapper = new QueryWrapper<>();
            equipmentImageQueryWrapper.eq("equipment_id",equipment.getEquipmentId());
            List<EquipmentImage> list1 = equipmentImageService.list(equipmentImageQueryWrapper);
            if (!list1.isEmpty()) {
                // 获取第一个设备图片
                EquipmentImage firstImage = list1.get(0);
                equipment.setImageUrl(firstImage.getImageUrl());
            }
        }
        return R.ok().data("list",list);
    }


    /**
     * 前台-根据登录的用户去查询自己出售的设备信息
     * */
    @GetMapping("/selectEquipmentByUserId/{id}/{pageNum}/{pageSize}")
    public  R  selectEquipmentByUserId(@PathVariable("id") Long id,@PathVariable("pageNum") Integer pageNum,
                                                    @PathVariable("pageSize") Integer pageSize){

        //根据用户id查询设备信息
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("user_id", id);
        equipmentQueryWrapper.orderByDesc("release_time");
        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        for (int i=0;i<list.size();i++){
            Equipment equipment = list.get(i);

            //根据设备类型id去查询设备类型表
            EquipmentType equipmentType = equipmentTypeService.getById(equipment.getTypeId());
            equipment.setTypeName(equipmentType.getTypeName());

            //根据设备id查询设备图片
            QueryWrapper<EquipmentImage> equipmentImageQueryWrapper = new QueryWrapper<>();
            equipmentImageQueryWrapper.eq("equipment_id",equipment.getEquipmentId());
            List<EquipmentImage> list1 = equipmentImageService.list(equipmentImageQueryWrapper);
            if (!list1.isEmpty()) {
                // 获取第一个设备图片
                EquipmentImage firstImage = list1.get(0);
                equipment.setImageUrl(firstImage.getImageUrl());
            }
        }

        int total = list.size();
        int startIndex = (pageNum - 1) * pageSize;

        // 边界检查：确保 startIndex 不超过总数据量
        if (startIndex >= total) {
            // 如果起始索引超出范围，返回空列表
            return R.ok()
                    .data("list", new ArrayList<>())
                    .data("total", total);
        }

        int endIndex = Math.min(startIndex + pageSize, total);
        List<Equipment> equipmentList = list.subList(startIndex, endIndex);

        return R.ok()
                .data("equipmentList", equipmentList)
                .data("total", total);
    }

    /**
     * 前台-根据设备id去修改设备的状态为已上架
     * */
    @PutMapping("/updateEquipmentStatusToOne/{id}")
    public  R updateEquipmentStatusToOne(@PathVariable("id") Integer id){

        Equipment byId = equipmentService.getById(id);

        byId.setStatus(1);

        boolean b = equipmentService.updateById(byId);

        if (b==true)
          return R.ok().code(20000).message("修改成功").data("success", true);
        return R.error().code(20001).message("修改失败").data("success", false);
    }


    /**
     * 前台-根据设备id去修改设备的状态为已下架
     * */
    @PutMapping("/updateEquipmentStatusToTwo/{id}")
    public  R updateEquipmentStatusToTwo(@PathVariable("id") Integer id){

        Equipment byId = equipmentService.getById(id);

        byId.setStatus(2);

        boolean b = equipmentService.updateById(byId);

        if (b==true)
            return R.ok().code(20000).message("修改成功").data("success", true);
        return R.error().code(20001).message("修改失败").data("success", false);
    }


    /**
     * 前台-根据设备id删除未审核的设备信息
     * */
    @DeleteMapping("/deleteEquipmentByStatusLing/{id}")
    public  R deleteEquipmentByStatusLing(@PathVariable("id") Integer id) {

        boolean b = equipmentService.removeById(id);

        if (b==true)
            return R.ok().code(20000).message("删除成功").data("success", true);
        return R.error().code(20001).message("删除失败").data("success", false);
}

    /**
     * 前台-查询前9个省份地区
     * */
    @GetMapping("/selectfrontbaEquipmentLocation")
    public R selectfrontbaEquipmentLocation() {

        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("status",1);
        equipmentQueryWrapper.select("DISTINCT location");
        equipmentQueryWrapper.last("LIMIT 9");

        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        return R.ok().data("list",list);
    }


    /**
     * 前台-查询全部设备省份地区
     * */
    @GetMapping("/selectallEquipmentLocation")
    public R selectallEquipmentLocation() {

        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("status",1);
        equipmentQueryWrapper.select("DISTINCT location");

        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        return R.ok().data("list",list);
    }
    /**
     * 前台-查询设备信息
     * */
    @PostMapping("/selectEquipmentInfo/{pageNum}/{pageSize}")
    public R selectEquipmentInfo(@RequestBody EquipmentVo vo,
                                 @PathVariable("pageNum") Integer pageNum,
                                 @PathVariable("pageSize") Integer pageSize){
       return equipmentService.selectEquipmentByType(vo,pageNum,pageSize);
    }

    /**
     * 后台-按条件查询设备信息
     * */
    @PostMapping("/selectEquipmentAll/{pageNo}/{pageSize}")
    public R selectEquipmentAll(@PathVariable("pageNo") Integer pageNo,
                                @PathVariable("pageSize") Integer pageSize,
                                @RequestBody EquipmentVo vo){
        return equipmentService.selectEquipmentByType(vo,pageNo,pageSize);
    }


    /**
     * 后台-根据设备Id返回对应设备详情
     * */
    @GetMapping("/selectEquipmentById/{id}")
    public R selectEquipmentById(@PathVariable("id") Long id){
        return equipmentService.selectEquipmentById(id);
    }

    /**
     * 远程调用接口查询指定设备详情
     */
    @GetMapping("/selectEquipmentByIdFeign/{id}")
    public Equipment selectEquipmentByIdFeign(@PathVariable("id") Long id){
        return equipmentService.selectEquipmentByIdFeign(id);
    }

    //查询设备对象
    @GetMapping("/selectEquipmentDxById/{id}")
    public Equipment selectEquipmentDxById(@PathVariable("id") Long id){
        return equipmentService.getById(id);
    }

    /**
     * 后台-审核设备
     * */
    @PutMapping("/updateEquipmentStatus/{id}/{status}")
    public R updateEquipmentStatus(@PathVariable("id") Long id,
                                   @PathVariable("status") Integer status)
        {
        return equipmentService.updateEquipmentStatus(id,status);
    }

    /**
     * 前台-根据条件设备类型，设备品牌，设备地区分页查询上架设备信息
     * */
    @PostMapping("/pageEquipmentByCondition/{pageNum}/{pageSize}")
    public R pageEquipmentByCondition(@PathVariable Integer pageNum,
                                      @PathVariable Integer pageSize,
                                      @RequestBody EquipmentVo equipmentVo) {
        //上架
        equipmentVo.setStatus("1");
        PageInfo<Equipment> equipmentPageInfo = equipmentService.pageEquipmentByVo(pageNum, pageSize, equipmentVo);
        return R.ok().data("list",equipmentPageInfo);
    }

    /**
     * 关联查询设备所有信息
     * @return
     */

    @GetMapping("/selectAllEquipmentInfo")
    public R selectAllEquipmentInfo() {
        List<Equipment> equipment = equipmentService.selectAllEquipmentInfo();
        return R.ok().data("list",equipment);
    }


    // 前台-我要买-详情
    @GetMapping("/selectEquipmentInfoById/{id}")
    public R selectEquipmentInfoById(@PathVariable("id") Long id){
        QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
        wrapper.eq("equipment_id",id);
        wrapper.in("status",1,3);
        Equipment equipment = equipmentService.getOne(wrapper);

        //根据设备id查询设备名称
        EquipmentType equipmentType = equipmentTypeService.getById(equipment.getTypeId());
        equipment.setTypeName(equipmentType.getTypeName());
        equipment.setTypeDesc(equipmentType.getTypeDesc());

        //根据设备id查询设备图片
        QueryWrapper<EquipmentImage> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("equipment_id",equipment.getEquipmentId());
        List<EquipmentImage> list = equipmentImageService.list(wrapper1);

        ArrayList<String> imageUrls = new ArrayList<>();
        for (EquipmentImage equipmentImage : list){
          imageUrls.add(equipmentImage.getImageUrl());
        }
        //根据设备id查询设备视频
        QueryWrapper<EquipmentVideo> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("equipment_id",equipment.getEquipmentId());
        List<EquipmentVideo> equipmentVideoList = equipmentVideoService.list(wrapper2);
        if (!equipmentVideoList.isEmpty()) {
            // 获取第一个设备视频
            EquipmentVideo equipmentVideo = equipmentVideoList.get(0);
            equipment.setVideoUrl(equipmentVideo.getVideoUrl());
        }
        equipment.setImages(imageUrls);
        return R.ok().data("list",equipment);
    }


    /**
     * 前台-根据设备id查询设备信息+设备图片信息+设备类型信息
     * */
    @PostMapping("/selectEquipmentAndImageAndType/{id}")
    public Equipment selectEquipmentAndImageAndType(@PathVariable("id") Long id, @RequestBody UserCollectionEquipmentVoPC userCollectionEquipmentVoPC) {

        //根据设备id查询设备信息
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("equipment_id", id);
        equipmentQueryWrapper.ne("status", 0);

        if (userCollectionEquipmentVoPC!=null){
            if (userCollectionEquipmentVoPC.getTypeId()!=null)
                equipmentQueryWrapper.eq("type_id",userCollectionEquipmentVoPC.getTypeId());
            if (!StringUtils.isEmpty(userCollectionEquipmentVoPC.getBrand()))
                equipmentQueryWrapper.eq("brand",userCollectionEquipmentVoPC.getBrand());
            if (userCollectionEquipmentVoPC.getMinPrice()!=null)
                equipmentQueryWrapper.ge("price",userCollectionEquipmentVoPC.getMinPrice());
            if (userCollectionEquipmentVoPC.getMaxPrice()!=null)
                equipmentQueryWrapper.le("price",userCollectionEquipmentVoPC.getMaxPrice());
        }

        Equipment equipment = equipmentService.getOne(equipmentQueryWrapper);

        if (equipment == null) {
            return null;
        }

        //根据设备类型id去查询设备类型表
        EquipmentType equipmentType = equipmentTypeService.getById(equipment.getTypeId());
        equipment.setTypeName(equipmentType.getTypeName());

        //根据设备id查询设备图片
        QueryWrapper<EquipmentImage> equipmentImageQueryWrapper = new QueryWrapper<>();
        equipmentImageQueryWrapper.eq("equipment_id", equipment.getEquipmentId());
        List<EquipmentImage> list1 = equipmentImageService.list(equipmentImageQueryWrapper);
        if (!list1.isEmpty()) {
            // 获取第一个设备图片
            EquipmentImage firstImage = list1.get(0);
            equipment.setImageUrl(firstImage.getImageUrl());
        }
        return equipment;
    }
    @PostMapping("/selectEquipmentAndImageAndType3/{id}")
    public Equipment selectEquipmentAndImageAndType3(@PathVariable("id") Long id, @RequestBody UserCollectionEquipmentVoPC userCollectionEquipmentVoPC) {

        //根据设备id查询设备信息
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("equipment_id", id);

        if (userCollectionEquipmentVoPC!=null){
            if (userCollectionEquipmentVoPC.getTypeId()!=null)
                equipmentQueryWrapper.eq("type_id",userCollectionEquipmentVoPC.getTypeId());
            if (!StringUtils.isEmpty(userCollectionEquipmentVoPC.getBrand()))
                equipmentQueryWrapper.like("brand",userCollectionEquipmentVoPC.getBrand());
            if (userCollectionEquipmentVoPC.getMinPrice()!=null)
                equipmentQueryWrapper.ge("price",userCollectionEquipmentVoPC.getMinPrice());
            if (userCollectionEquipmentVoPC.getMaxPrice()!=null)
                equipmentQueryWrapper.le("price",userCollectionEquipmentVoPC.getMaxPrice());
        }

        Equipment equipment = equipmentService.getOne(equipmentQueryWrapper);

        if (equipment == null) {
            return null;
        }

        //根据设备类型id去查询设备类型表
        EquipmentType equipmentType = equipmentTypeService.getById(equipment.getTypeId());
        equipment.setTypeName(equipmentType.getTypeName());

        //根据设备id查询设备图片
        QueryWrapper<EquipmentImage> equipmentImageQueryWrapper = new QueryWrapper<>();
        equipmentImageQueryWrapper.eq("equipment_id", equipment.getEquipmentId());
        List<EquipmentImage> list1 = equipmentImageService.list(equipmentImageQueryWrapper);
        if (!list1.isEmpty()) {
            // 获取第一个设备图片
            EquipmentImage firstImage = list1.get(0);
            equipment.setImageUrl(firstImage.getImageUrl());
            equipment.setEquipmentImages(list1);
        }
        return equipment;
    }



    @GetMapping("/selectEqById/{id}")
    public Equipment selectEqById(@PathVariable("id") Long id){
        LambdaQueryWrapper<Equipment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Equipment::getEquipmentId,id);
        return equipmentService.getOne(wrapper);
    }


    //查询设备数据-我要买-详情-设备预定
    @GetMapping("/selectEquipmentInfoXiangQingById/{id}")
    public R selectEquipmentInfoXiangQingById(@PathVariable("id") Long id){
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("equipment_id",id);
        Equipment list = equipmentService.getOne(queryWrapper);

        //根据设备id查询设备名称
        EquipmentType equipmentType = equipmentTypeService.getById(list.getTypeId());
        list.setTypeName(equipmentType.getTypeName());

        return R.ok().data("list",list);
    }



    /**
     * 前台-根据设备类型id查询下面的品牌名称
     * */
    @GetMapping("/selectEquipmentInfoByTypeId/{id}")
    public  R  selectEquipmentInfoByTypeId(@PathVariable("id") Integer id) {

        //查询设备信息
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("type_id", id);
        equipmentQueryWrapper.select("DISTINCT brand");

        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        return R.ok().data("list", list);
    }


    /**
     * 前台-点击设备品牌去查询下面的品牌型号
     * */
    @GetMapping("/selectEquipmentInfoByBrandName/{brand}")
    public  R  selectEquipmentInfoByBrandName(@PathVariable("brand") String brand) {

        //查询设备信息
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("brand", brand);
        equipmentQueryWrapper.select("DISTINCT model");

        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        return R.ok().data("list", list);
    }


    /**
     * 前台-查询所有的支腿形式
     * */
    @GetMapping("/selectAllEquipmentoutriggerType")
    public  R  selectAllEquipmentoutriggerType() {

        //查询设备信息
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("status", 1);
        equipmentQueryWrapper.select("DISTINCT outrigger_type");

        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        return R.ok().data("list", list);
    }


    /**
     * 前台-查询所有的排放标准
     * */
    @GetMapping("/selectAllEquipmentemissionStandard")
    public  R  selectAllEquipmentemissionStandard() {

        //查询设备信息
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("status", 1);
        equipmentQueryWrapper.select("DISTINCT emission_standard");

        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);

        return R.ok().data("list", list);
    }


    /**
     * 前台-我的设备里面去编辑未审核的设备信息
     * */
    @PostMapping("/editEquipmentByStatusLing")
    public  R  editEquipmentByStatusLing(@RequestBody EquipmentEditDtoPC equipmentEditDtoPC) {

        if (equipmentEditDtoPC.getEquipment()!=null){

            //拿到传递的设备对象
            Equipment equipment = equipmentEditDtoPC.getEquipment();

            //先去编辑设备信息
            Equipment equipmentDx = new Equipment();
            equipmentDx.setEquipmentId(equipment.getEquipmentId());
            equipmentDx.setTypeId(equipment.getTypeId());
            equipmentDx.setBrand(equipment.getBrand());
            equipmentDx.setModel(equipment.getModel());
            equipmentDx.setYear(equipment.getYear());
            equipmentDx.setHour(equipment.getHour());
            equipmentDx.setPrice(equipment.getPrice());
            equipmentDx.setVerticalHeight(equipment.getVerticalHeight());
            equipmentDx.setChassisAxle(equipment.getChassisAxle());
            equipmentDx.setOutriggerType(equipment.getOutriggerType());
            equipmentDx.setEmissionStandard(equipment.getEmissionStandard());
            equipmentDx.setLocation(equipment.getLocation());
            equipmentDx.setContactName(equipment.getContactName());
            equipmentDx.setContactPhone(equipment.getContactPhone());
            equipmentDx.setReleaseTime(LocalDateTime.now());
            equipmentDx.setUpdateTime(LocalDateTime.now());
             //修改
             equipmentService.updateById(equipmentDx);

             //设备图片集合
            if (equipmentEditDtoPC.getEquiImageDtoList()!=null){

                QueryWrapper<EquipmentImage> equipmentImageQueryWrapper = new QueryWrapper<>();
                equipmentImageQueryWrapper.eq("equipment_id",equipment.getEquipmentId());
                //先根据设备id清空全部的设备图片
                boolean b = equipmentImageService.remove(equipmentImageQueryWrapper);

                //判断封面图片
                int index = 0;
                for (EquiImageDto equiImage : equipmentEditDtoPC.getEquiImageDtoList()) {

                     if (b==true){
                         //重新添加设备图片
                         EquipmentImage equipmentImage = new EquipmentImage();
                         equipmentImage.setEquipmentId(equipment.getEquipmentId());
                         equipmentImage.setImageUrl(equiImage.getUrl());
                         equipmentImage.setIsCover(index == 0 ? 1 : 0); // 第一张为封面
                         equipmentImage.setCreateTime(LocalDateTime.now());
                         index++;
                         //添加设备图片
                         equipmentImageService.save(equipmentImage);
                     }
                 }

                //判断设备视频1
                if (equipmentEditDtoPC.getEquilVideoDto1()!=null){

                    //先根据设备id删除设备视频对象
                    QueryWrapper<EquipmentVideo> equipmentVideoQueryWrapper = new QueryWrapper<>();
                    equipmentVideoQueryWrapper.eq("equipment_id",equipment.getEquipmentId());
                    boolean remove = equipmentVideoService.remove(equipmentVideoQueryWrapper);

                    if (remove==true){
                        EquilVideoDto1 equilVideoDto1 = equipmentEditDtoPC.getEquilVideoDto1();

                        EquipmentVideo equipmentVideo = new EquipmentVideo();
                        equipmentVideo.setEquipmentId(equipment.getEquipmentId());
                        equipmentVideo.setVideoUrl(equilVideoDto1.getUrl());
                        equipmentVideo.setVideoName(equilVideoDto1.getName());
                        equipmentVideo.setVideoSize(Long.valueOf(equilVideoDto1.getFileSize()));
                        equipmentVideo.setCreateTime(LocalDateTime.now());

                        //添加设备视频
                        equipmentVideoService.save(equipmentVideo);
                    }
                }

                //判断设备视频2
                if (equipmentEditDtoPC.getEquilVideoDto2()!=null) {
                    //先根据设备id删除设备视频对象
                    QueryWrapper<EquipmentVideo> equipmentVideoQueryWrapper = new QueryWrapper<>();
                    equipmentVideoQueryWrapper.eq("equipment_id", equipment.getEquipmentId());
                    boolean remove = equipmentVideoService.remove(equipmentVideoQueryWrapper);

                    if (remove == true) {
                        EquilVideoDto2 equilVideoDto2 = equipmentEditDtoPC.getEquilVideoDto2();

                        EquipmentVideo equipmentVideo = new EquipmentVideo();
                        equipmentVideo.setEquipmentId(equipment.getEquipmentId());
                        equipmentVideo.setVideoUrl(equilVideoDto2.getUrl());
                        equipmentVideo.setVideoName(equilVideoDto2.getName());
                        equipmentVideo.setVideoSize(Long.valueOf(equilVideoDto2.getSize()));
                        equipmentVideo.setCreateTime(LocalDateTime.now());
                        //添加设备视频
                        equipmentVideoService.save(equipmentVideo);
                    }
                }
            }
        }

        return R.ok().code(20000).message("编辑成功").data("success", true);
    }

    /**
     * 前台-我要卖上传设备信息
     * */
    @PostMapping("/saveEquipmentInfoPC")
    public  R  saveEquipmentInfoPC(@RequestBody EquipmentEditDtoPC equipmentEditDtoPC) {

        if (equipmentEditDtoPC.getEquipment()!=null){
            //拿到传递的设备对象
            Equipment equipment = equipmentEditDtoPC.getEquipment();

            User byId = myUserFeign.selectUserById(equipment.getUserId());
            if (byId.getUserType()==0){
                myUserFeign.updateUserStatus(byId.getUserId());
            }


            //先去编辑设备信息
            Equipment equipmentDx = new Equipment();
            equipmentDx.setUserId(equipment.getUserId());
            equipmentDx.setTypeId(equipment.getTypeId());
            equipmentDx.setBrand(equipment.getBrand());
            equipmentDx.setModel(equipment.getModel());
            equipmentDx.setYear(equipment.getYear());
            equipmentDx.setHour(equipment.getHour());
            equipmentDx.setPrice(equipment.getPrice());
            equipmentDx.setVerticalHeight(equipment.getVerticalHeight());
            equipmentDx.setChassisAxle(equipment.getChassisAxle());
            equipmentDx.setOutriggerType(equipment.getOutriggerType());
            equipmentDx.setEmissionStandard(equipment.getEmissionStandard());
            equipmentDx.setLocation(equipment.getLocation());
            equipmentDx.setContactName(equipment.getContactName());
            equipmentDx.setContactPhone(equipment.getContactPhone());
            equipmentDx.setReleaseTime(LocalDateTime.now());
            equipmentDx.setCreateTime(LocalDateTime.now());
            equipmentDx.setUpdateTime(LocalDateTime.now());
            equipmentDx.setStatus(0);
            equipmentDx.setIsDetected(0);
            //添加
            equipmentService.save(equipmentDx);

            //设备图片集合
            if (equipmentEditDtoPC.getEquiImageDtoList()!=null){
                //判断封面图片
                int index = 0;
                for (EquiImageDto equiImage : equipmentEditDtoPC.getEquiImageDtoList()) {
                        //重新添加设备图片
                        EquipmentImage equipmentImage = new EquipmentImage();
                        equipmentImage.setEquipmentId(equipmentDx.getEquipmentId());
                        equipmentImage.setImageUrl(equiImage.getUrl());
                        equipmentImage.setIsCover(index == 0 ? 1 : 0); // 第一张为封面
                        equipmentImage.setCreateTime(LocalDateTime.now());
                        index++;
                        //添加设备图片
                        equipmentImageService.save(equipmentImage);
                    }

                //判断设备视频
                if (equipmentEditDtoPC.getEquilVideoDto2()!=null) {
                        EquilVideoDto2 equilVideoDto2 = equipmentEditDtoPC.getEquilVideoDto2();

                        EquipmentVideo equipmentVideo = new EquipmentVideo();
                        equipmentVideo.setEquipmentId(equipmentDx.getEquipmentId());
                        equipmentVideo.setVideoUrl(equilVideoDto2.getUrl());
                        equipmentVideo.setVideoName(equilVideoDto2.getName());
                        equipmentVideo.setVideoSize(Long.valueOf(equilVideoDto2.getSize()));
                        equipmentVideo.setCreateTime(LocalDateTime.now());
                        //添加设备视频
                        equipmentVideoService.save(equipmentVideo);
                     }
                }
        }

        return R.ok().code(20000).message("添加成功").data("success", true);
    }


    /**
     * 前台-根据设备id查询设备信息+设备图片信息+设备类型信息,不带查询条件
     * */
    @PostMapping("/selectEquipmentAndImageAndType2/{id}")
    public Equipment selectEquipmentAndImageAndType2(@PathVariable("id") Long id) {

        //根据设备id查询设备信息
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("equipment_id", id);

        Equipment equipment = equipmentService.getOne(equipmentQueryWrapper);

        if (equipment == null) {
            return null;
        }

        //根据设备类型id去查询设备类型表
        EquipmentType equipmentType = equipmentTypeService.getById(equipment.getTypeId());
        equipment.setTypeName(equipmentType.getTypeName());

        //根据设备id查询设备图片
        QueryWrapper<EquipmentImage> equipmentImageQueryWrapper = new QueryWrapper<>();
        equipmentImageQueryWrapper.eq("equipment_id", equipment.getEquipmentId());
        List<EquipmentImage> list1 = equipmentImageService.list(equipmentImageQueryWrapper);
        if (!list1.isEmpty()) {
            // 获取第一个设备图片
            EquipmentImage firstImage = list1.get(0);
            equipment.setImageUrl(firstImage.getImageUrl());
        }
        return equipment;
    }


    /**
     * 前台-修改设备的用户id还有设备状态根据设备id
     * */
    @PutMapping("/editEquipmentUserId/{id}/{equipmentId}")
    public String editEquipmentUserId(@PathVariable("id") Long id,@PathVariable("equipmentId") Long equipmentId) {

        //根据设备id查询设备信息
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.eq("equipment_id", equipmentId);

        Equipment equipment = equipmentService.getOne(equipmentQueryWrapper);

        if (equipment == null) {
            return null;
        }

        equipment.setUserId(id);
        equipment.setStatus(3);

        boolean b = equipmentService.updateById(equipment);

        if (b==true)
            return "ok";
        return "no";
    }

    @PostMapping("/editEquipmentByStatusLing/{BId}")
    public List<Equipment> editEquipmentByStatusLing(@PathVariable("BId") Integer BId,@RequestBody BusinessmanEquipmentVo  vo) {
        return  equipmentService.selectEquipmentByBusinVo(BId,vo);
    }


    @GetMapping("/selectEqById2/{eid}/{isId}")
    public Equipment selectEqById2(@PathVariable("eid") Long eid,@PathVariable("isId") Long isId) {
        LambdaQueryWrapper<Equipment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Equipment::getEquipmentId,eid);
        wrapper.eq(Equipment::getIsDetected,isId);
        return equipmentService.getOne(wrapper);
    }

    @PostMapping("/selectEq/{eid}")
    public Equipment selectEq(@PathVariable("eid") Long eid,@RequestBody BusinessmanEquipmentVo vo) {
        LambdaQueryWrapper<Equipment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Equipment::getEquipmentId,eid);
        if (vo!=null){
            if (vo.getSearchIsDetected() != null){
                wrapper.eq(Equipment::getIsDetected,vo.getSearchIsDetected());
            }
            if (vo.getSearchEquipmentName()!= null){
                wrapper.like(Equipment::getBrand,vo.getSearchEquipmentName());
            }
            if (vo.getEquipmentType() != null){
                wrapper.eq(Equipment::getTypeId,vo.getEquipmentType());
            }
        }
        Equipment one = equipmentService.getOne(wrapper);
        if (one != null)
            return one;
        return null;
    }


    //前台-交易完全修改设备状态是已成交
    @PutMapping("/editequipmentStatusChengJiao/{id}")
    public String editequipmentStatusChengJiao(@PathVariable("id") Long id){
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("equipment_id",id);
        Equipment equipment = equipmentService.getOne(queryWrapper);
        equipment.setStatus(3);

        boolean b = equipmentService.updateById(equipment);

         if (b==true)
                return "ok";
           return "no";
    }
    /**
     * 前台-我要求购，查询所有品牌
     * */
    @GetMapping("/selectQuiGouEquipmentBrandPC")
    public R selectQuiGouEquipmentBrandPC() {
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.select("DISTINCT brand");
        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);
        return R.ok().data("list", list);
    }
    /**
     * 前台-我要求购，查询所有型号
     * */
    @GetMapping("/selectQuiGouEquipmentModelPC")
    public R selectQuiGouEquipmentModelPC() {
        QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
        equipmentQueryWrapper.select("DISTINCT model");
        List<Equipment> list = equipmentService.list(equipmentQueryWrapper);
        return R.ok().data("list", list);
    }

}
